package
{
	import com.bit101.components.HBox;
	import com.bit101.components.HUISlider;
	import com.bit101.components.List;
	import com.bit101.components.PushButton;
	import com.bit101.components.Style;
	import com.bit101.components.Text;
	import com.bit101.components.TextArea;
	import com.bit101.components.VBox;
	import com.bit101.components.VScrollBar;
	import com.bit101.components.Window;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.FileFilter;
	import flash.utils.ByteArray;
	
	import info.BAAnimationInfo;
	
	
	public class ValuePanel extends Sprite
	{
		public function ValuePanel()
		{
			super();
		}
		
		//人物是否已经加载
		private var loadComplete:Boolean=false;
		
		private var optionBox:HBox;
		
		public var logPanel:TextArea;
		
		public var preView:VBox;
		
		public function init():void
		{
			Style.setStyle(Style.DARK);
			Style.embedFonts = false;
			Style.fontName = "consolas";  
			Style.fontSize=12;
			
			optionBox=new HBox();
			addChild(optionBox);
			optionBox.x=300;
			optionBox.y=Setting.HEIGHT-Setting.PANEL_HEIGHT;
			
			var logWin:Window=new Window();
			logWin.title="Log Info";
			logWin.draggable=false;
			logWin.width=295;
			logWin.height=Setting.HEIGHT-400;
			addChild(logWin);
			logPanel=new TextArea();
			logPanel.width=295;
			logPanel.height=Setting.HEIGHT-400;
			logPanel.editable=false;
			logWin.addChild(logPanel);
			
			//initialize log
			Log.init(logPanel);
			
			initPreViewWindows();
			
			initResourceLoad();
			
			initBaseSetting();
		}
		
		/**Load Resource**/
		private function initResourceLoad():void
		{
			var resWin:Window=new Window(this,0,0,"Resource Load");
			resWin.draggable=false;
			resWin.width=330;
			resWin.height=Setting.PANEL_HEIGHT;
			
			var textHBox:HBox=new HBox();
			var animateConfigText:Text=new Text();
			animateConfigText.width=120;
			animateConfigText.height=20;
			animateConfigText.text="load skeleton animate resource";
			animateConfigText.enabled=false;
			
			var boneFile:File=new File();
			var boneData:ByteArray;
			var browserBtn:PushButton=new PushButton();
			browserBtn.label="Browser";
			browserBtn.addEventListener(MouseEvent.CLICK,browserBtnOnClick);
			function browserBtnOnClick(e:MouseEvent):void
			{
				var fileFilter:FileFilter=new FileFilter("Bone Animation(*.ba)","*.ba");
				boneFile.addEventListener(Event.SELECT,function(evt:Event):void{
					animateConfigText.text=boneFile.nativePath;
					var newFile:File = evt.target as File;
					if(newFile.exists)
					{
						var stream:FileStream = new FileStream();
						stream.addEventListener(Event.COMPLETE,function(e:Event):void
						{
							Log.info("skeleton animation load complete.");
							boneData=new ByteArray();
							boneData.position=0;
							stream.readBytes(boneData,0,stream.bytesAvailable);
							stream.close();
						});
						stream.addEventListener(IOErrorEvent.IO_ERROR,function(e:Event):void
						{
							Log.info("skeleton animation load IO Error.");
						});
					}else
						Log.error("file not exist.");
					stream.openAsync(newFile,FileMode.READ);
				});
				boneFile.browse([fileFilter]);
			}
			var confirmBtn:PushButton=new PushButton();
			confirmBtn.label="Confirm";
			confirmBtn.addEventListener(MouseEvent.CLICK,confirmBtnOnClick);
			function confirmBtnOnClick(e:MouseEvent):void
			{
				var baInfo:BAAnimationInfo;
				if(boneData)
				{
					boneData.uncompress();
					baInfo=new BAAnimationInfo(boneData);
					ResourceMgr.addPlayer(baInfo);
					var bitmapData:BitmapData=Utils.snapshootFromObject(ResourceMgr.playerLsit[baInfo]);
					for (var i:int = 0; i <10; i++) 
					{
						var bitmap:Bitmap=new Bitmap(bitmapData);
						addPreView(bitmap);
					}
				}
				else
					Log.error("please load skeleton animation file.");
				trace();
			}
			
			textHBox.addChild(animateConfigText);
			textHBox.addChild(browserBtn);
			textHBox.addChild(confirmBtn);
			resWin.addChild(textHBox);
			optionBox.addChild(resWin);
		}
		
		private var vScrollBar:VScrollBar;
		/**preView windows**/
		private function initPreViewWindows():void
		{
			var preViewWIn:Window=new Window(this,0,0,"PreView Winodws");
			preViewWIn.draggable=false;
			preViewWIn.width=295;
			preViewWIn.height=Setting.PRE_VIEW_HEIGHT;
			preViewWIn.y=0;
			
			
			preView=new VBox(); 
			preView.width=295;
			preView.height=Setting.PRE_VIEW_HEIGHT;
			preViewWIn.addChild(preView); 
			
			vScrollBar=new VScrollBar(preView,500,0,onScrollPreView);
			addChild(vScrollBar);
			addChild(preViewWIn);
		}
		
		private function onScrollPreView(e:Event):void
		{
			trace(vScrollBar.value);
			var persent:Number=vScrollBar.value/vScrollBar.maximum;
			trace(persent);
			preView.y=-preView.height*persent;
			trace(preView.y);
		}
		
		/**Base Setting**/
		private function initBaseSetting():void
		{
			var baseSetWIn:Window=new Window(this,0,0,"Base Setting");
			baseSetWIn.draggable=false;
			baseSetWIn.width=230;
			baseSetWIn.height=Setting.PANEL_HEIGHT;
			
			var vBox:VBox=new VBox(); 
			vBox.alignment=VBox.CENTER;
			vBox.width=230;
			baseSetWIn.addChild(vBox);
			optionBox.addChild(baseSetWIn); 
			for (var propertyName:String in Setting.baseValue) 
			{
				var hslider:HUISlider=new HUISlider();
				hslider.minimum=50;
				hslider.maximum=500;
				hslider.label=propertyName;
				hslider.value=Setting.baseValue[propertyName];
				vBox.addChild(hslider);
			}
		}
		
		private function addPreView(target:DisplayObject):void
		{
			var numChildren:uint=preView.numChildren+1;
			var contentHeight:Number = numChildren * (target.height+5);
			var pageSize:Number = Math.floor(Setting.PRE_VIEW_HEIGHT / target.height);
			
			vScrollBar.setThumbPercent(Setting.HEIGHT / contentHeight); 
			vScrollBar.pageSize=pageSize;
			vScrollBar.setSize(295,Setting.HEIGHT);
			vScrollBar.minimum=0;
			vScrollBar.maximum=preView.height-Setting.HEIGHT;
			preView.addChild(target);
		}
	}
}